<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="配置项名称" prop="attrName">
        <el-input
          v-model="queryParams.attrName"
          placeholder="请输入配置项名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="计算服务名称" prop="salaryCalculateService">
        <el-input
          v-model="queryParams.salaryCalculateService"
          placeholder="请输入计算服务名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['ehr:salary_attr_config:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['ehr:salary_attr_config:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['ehr:salary_attr_config:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['ehr:salary_attr_config:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="salary_attr_configList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="配置项ID" align="left" prop="attrId" />
      <el-table-column label="配置项名称" align="left" prop="attrName" />
      <el-table-column label="配置项描述" align="left" prop="description" />
      <el-table-column label="是否计算服务" align="left" prop="isCalculateService" :formatter="isCalculateServiceFormat"/>
      <el-table-column label="计算服务名称" align="left" prop="salaryCalculateService"  width="180" />
      <el-table-column label="固定金额" align="left" prop="salaryAmount" />
      <el-table-column label="计算参数" align="left" prop="calculateParams" />
      <el-table-column label="是否适用全体" align="left" prop="isPublic" :formatter="isPublicFormat"/>
  <el-table-column label="最新更新时间" align="center" prop="lastUpdatedStamp" width="120">
    <template slot-scope="scope">
      <span>{{ parseTime(scope.row.lastUpdatedStamp, '{y}-{m}-{d} {h}:{i}:{s}') }}</span>
    </template>
  </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['ehr:salary_attr_config:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['ehr:salary_attr_config:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改薪资项服务配置对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="配置项名称" prop="attrName">
          <el-input v-model="form.attrName" placeholder="请输入配置项名称" />
        </el-form-item>
        <el-form-item label="配置项描述" prop="description">
          <el-input v-model="form.description" placeholder="请输入配置项描述" />
        </el-form-item>
  <el-form-item label="是否计算服务" prop="isCalculateService">
    <el-select v-model="form.isCalculateService" placeholder="请选择是否需要计算服务">
      <el-option
          v-for="dict in isCalculateServiceOptions"
          :key="dict.enumId"
          :label="dict.description"
          :value="dict.enumId"
      ></el-option>
    </el-select>
  </el-form-item>
        <el-form-item label="计算服务名称" prop="salaryCalculateService">
          <el-input v-model="form.salaryCalculateService" placeholder="请输入计算服务名称" />
        </el-form-item>
        <el-form-item label="固定金额" prop="salaryAmount">
          <el-input v-model="form.salaryAmount" placeholder="请输入固定金额" />
        </el-form-item>
        <el-form-item label="计算参数" prop="calculateParams">
          <el-input v-model="form.calculateParams" placeholder="请输入计算参数" />
        </el-form-item>
  <el-form-item label="是否适用全体" prop="isPublic">
    <el-select v-model="form.isPublic" placeholder="请选择是否使用全体人员">
      <el-option
          v-for="dict in isPublicOptions"
          :key="dict.enumId"
          :label="dict.description"
          :value="dict.enumId"
      ></el-option>
    </el-select>
  </el-form-item>
      <el-form-item label="最新更新时间" prop="lastUpdatedStamp">
        <el-date-picker clearable
                        v-model="form.lastUpdatedStamp"
                        type="datetime"
                        value-format="yyyy-MM-dd HH:mm:ss"
                        default-time = "00:00:00"
                        placeholder="请选择最新更新时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="" prop="lastUpdatedTxStamp">
        <el-date-picker clearable
                        v-model="form.lastUpdatedTxStamp"
                        type="datetime"
                        value-format="yyyy-MM-dd HH:mm:ss"
                        default-time = "00:00:00"
                        placeholder="请选择">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="创建时间" prop="createdStamp">
        <el-date-picker clearable
                        v-model="form.createdStamp"
                        type="datetime"
                        value-format="yyyy-MM-dd HH:mm:ss"
                        default-time = "00:00:00"
                        placeholder="请选择创建时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="" prop="createdTxStamp">
        <el-date-picker clearable
                        v-model="form.createdTxStamp"
                        type="datetime"
                        value-format="yyyy-MM-dd HH:mm:ss"
                        default-time = "00:00:00"
                        placeholder="请选择">
        </el-date-picker>
      </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listSalary_attr_config, getSalary_attr_config, delSalary_attr_config, addSalary_attr_config, updateSalary_attr_config } from "@/api/ehr/salary_attr_config";

export default {
  name: "Salary_attr_config",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 薪资项服务配置表格数据
      salary_attr_configList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 是否需要计算服务业务字典
      isCalculateServiceOptions: [],
      // 是否使用全体人员业务字典
      isPublicOptions: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        attrName: null,
        salaryCalculateService: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }
    };
  },
  created() {
    // 是否需要计算服务业务字典
    this.getEnumDicts("YESORNO").then((response) => {
      this.isCalculateServiceOptions = response.data;
    });
    // 是否使用全体人员业务字典
    this.getEnumDicts("YESORNO").then((response) => {
      this.isPublicOptions = response.data;
    });
    this.getList();
  },
  methods: {
     isCalculateServiceFormat(row, column) {
      let val = null;
      this.isCalculateServiceOptions.forEach((item, index) => {
        if (row.isCalculateService === item.enumId) {
          val = item.description;
        }
      });
      return val;
    },
     isPublicFormat(row, column) {
      let val = null;
      this.isPublicOptions.forEach((item, index) => {
        if (row.isPublic === item.enumId) {
          val = item.description;
        }
      });
      return val;
    },
    /** 查询薪资项服务配置列表 */
    getList() {
      this.loading = true;
      listSalary_attr_config(this.queryParams).then(response => {
        this.salary_attr_configList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        attrId: null,
        attrName: null,
        description: null,
        isCalculateService: null,
        salaryCalculateService: null,
        salaryAmount: null,
        calculateParams: null,
        isPublic: null,
        lastUpdatedStamp: null,
        lastUpdatedTxStamp: null,
        createdStamp: null,
        createdTxStamp: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.attrId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加薪资项服务配置";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const attrId = row.attrId || this.ids
      getSalary_attr_config(attrId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改薪资项服务配置";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.attrId != null) {
            updateSalary_attr_config(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addSalary_attr_config(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const attrIds = row.attrId || this.ids;
      this.$modal.confirm('是否确认删除薪资项服务配置编号为"' + attrIds + '"的数据项？').then(function() {
        return delSalary_attr_config(attrIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('ehr/salary_attr_config/export', {
        ...this.queryParams
      }, `salary_attr_config_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
